package com.stylefeng.guns.rest.common.util;

import com.alibaba.fastjson.JSON;
import com.stylefeng.guns.core.util.MD5Util;
import com.stylefeng.guns.rest.modular.auth.converter.BaseTransferEntity;
import com.stylefeng.guns.rest.modular.auth.security.impl.Base64SecurityAction;
import com.stylefeng.guns.rest.pojo.EmailObject;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yuzih
 * @create 2018-05-25 11:27
 * @desc 数据加解密
 **/
public class DecryptUtil {
    /***
     *  数据解密
     * @param o 数据对象
     * @param salt 加密盐
     * @return
     */
    public static BaseTransferEntity decryptObject(Object o,String salt){
        String jsonString = JSON.toJSONString(o);
        String encode = new Base64SecurityAction().doAction(jsonString);
        String md5 = MD5Util.encrypt(encode + salt);

        BaseTransferEntity baseTransferEntity = new BaseTransferEntity();
        baseTransferEntity.setObject(encode);
        baseTransferEntity.setSign(md5);

        return baseTransferEntity;
    }

    public static void main(String[] args) {
        EmailObject o = new EmailObject();
        o.setContent("这是内容");
        o.setEmailType("qq");
        o.setHostAddress("103622095@qq.com");
        o.setHostPassword("123465");
        List<String> addresses = new ArrayList<>();
        addresses.add("yuzihao1995@qq.com");
        addresses.add("yuzihao1994@qq.com");
        addresses.add("yuzihao1993@qq.com");
        o.setTargetAddresses(addresses);
        o.setTitle("这是标题");
        System.out.println(DecryptUtil.decryptObject(o, "mra7mt"));
    }
}
